package private

import (
	"dc-go/global"
	"dc-go/model"
	"errors"
)

type ServiceApi struct{}

func (s *ServiceApi) GetApiList(getApiList model.RequestGetApiList) (role interface{}, total int64, err error) {
	pageSize := getApiList.PageSize
	offset := getApiList.PageSize * (getApiList.Page - 1)
	db := global.DcDb.Model(&model.SysApi{})
	var apiList []model.SysApi
	//配置搜索
	if getApiList.ApiGroup != "" {
		db = db.Where("api_group like ?", "%"+getApiList.ApiGroup+"%")
	}
	if getApiList.ApiMethod != "" {
		db = db.Where("api_method like ?", "%"+getApiList.ApiMethod+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(pageSize).Offset(offset).Order(model.OrderByColumn(getApiList.SortBy, getApiList.Desc)).Find(&apiList).Error
	return apiList, total, err
}

func (s *ServiceApi) EditApi(toEditApi model.SysApi) (err error) {
	// 因为前台只传 custom 字段, 这里允许编辑
	err = global.DcDb.Save(&toEditApi).Error
	return err
}

func (s *ServiceApi) AddApi(toAddApi model.SysApi) (err error) {
	err = global.DcDb.Create(&toAddApi).Error
	return err
}

func (s *ServiceApi) DeleteApiById(id uint) (err error) {
	var sysApi model.SysApi
	if err = global.DcDb.Where("id = ?", id).First(&sysApi).Error; err != nil {
		return err
	}
	if *sysApi.Source == "internal" {
		return errors.New("系统内置不允许删除")
	}
	err = global.DcDb.Where("id = ?", id).Unscoped().Delete(&sysApi).Error
	return err
}

func (s *ServiceApi) QueryApiById(id uint) (err error, roleInfo model.SysApi) {
	var role model.SysApi
	err = global.DcDb.First(&role, "id = ?", id).Error
	return err, role
}
